package cc.caiguang.png2ico;

import com.formdev.flatlaf.FlatDarkLaf;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        FlatDarkLaf.setup();
        Win win = new Win();
        JLabel label = win.getSrcLabel();
        new DropTarget(label, DnDConstants.ACTION_COPY_OR_MOVE, new DropTargetAdapter() {
            @Override
            public void drop(DropTargetDropEvent dtde)//重写适配器的drop方法
            {
                try {
                    if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor))//如果拖入的文件格式受支持
                    {
                        dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);//接收拖拽来的数据
                        List<File> list = (List<File>) dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
                        File file = list.get(0);
                        if(file == null || !getFileExtension(file).equals("png")) {
                            JOptionPane.showMessageDialog(null, "只能拖入一个PNG格式的图片！");
                        } else {

                            BufferedImage image = ImageIO.read(file);
                            if(image != null && image.getHeight(null) != image.getWidth(null)) {
                                JOptionPane.showMessageDialog(null, "需要正方形的图片！");
                            } else {
                                label.setText(null);
                                label.setBorder(null);
                                BufferedImage bufferedImage = Win.scaleImage(image, 256, 256);
                                label.setIcon(new ImageIcon(bufferedImage));
                                win.setImage(image);
//                                String path = file.getAbsolutePath();
//                                JOptionPane.showMessageDialog(null, path);
                            }

                        }

                        dtde.dropComplete(true);//指示拖拽操作已完成

                    } else {
                        dtde.rejectDrop();//否则拒绝拖拽来的数据
                    }

                } catch (IOException e) {
                    throw new RuntimeException(e);
                } catch (UnsupportedFlavorException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        win.setVisible(true);
    }

    public static String getFileExtension(File file) {
        if (file == null || !file.isFile()) {
            return null;
        }
        String fileName = file.getName();
        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex == -1 || dotIndex == fileName.length() - 1) {
            return null; // 没有扩展或扩展只是一个点
        }
        return fileName.substring(dotIndex + 1).toLowerCase(); // 为保持一致性，返回小写扩展名
    }
}